package com.timi_licai.timi_licai_account.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class CalendarGridViewAdapter extends BaseAdapter {

    private Calendar calStartDate = Calendar.getInstance();// 当前显示的日历
    private Calendar calToday = Calendar.getInstance(); // 今日
    private int iMonthViewCurrentMonth = 0; // 当前视图月
    List<Date> titles;
    private Context context;
    Resources resources;

    public CalendarGridViewAdapter(Context context, Calendar cal,
                                   List<Date> list, int iMonthViewCurrentMonth) {
        calStartDate = cal;
        this.context = context;
        resources = context.getResources();
        titles = list;
        this.iMonthViewCurrentMonth = iMonthViewCurrentMonth;
    }

    public CalendarGridViewAdapter(Context context) {
        this.context = context;
        resources = context.getResources();
    }


    @Override
    public int getCount() {
        return titles.size();
    }

    @Override
    public Object getItem(int position) {
        return titles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("deprecation")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout iv = new LinearLayout(context);
        iv.setGravity(Gravity.CENTER);
        iv.setOrientation(LinearLayout.VERTICAL);
        iv.setBackgroundColor(Color.parseColor("#ffffffff"));

        Date myDate = (Date) getItem(position);
        Calendar calCalendar = Calendar.getInstance();
        calCalendar.setTime(myDate);

        final int iMonth = calCalendar.get(Calendar.MONTH);
        final int iDay = calCalendar.get(Calendar.DAY_OF_WEEK);


        // 判断周六周日
        iv.setBackgroundColor(Color.parseColor("#ffffffff"));
        if (iDay == 7) {
            // 周六
            iv.setBackgroundColor(Color.parseColor("#ffffffff"));
        } else if (iDay == 1) {
            // 周日
            iv.setBackgroundColor(Color.parseColor("#ffffffff"));
        } else {

        }
        // 判断周六周日结束

        TextView txtToDay = new TextView(context);
        txtToDay.setGravity(Gravity.CENTER_HORIZONTAL);
        txtToDay.setTextSize(9);
        if (equalsDate(calToday.getTime(), myDate)) {
            // 当前日期
            iv.setBackgroundColor(Color.parseColor("#b1dce2ff"));
            txtToDay.setText("TODAY!");
        }
        // 设置背景颜色结束

        // 日期开始
        TextView txtDay = new TextView(context);// 日期
        txtDay.setGravity(Gravity.CENTER_HORIZONTAL);

        // 判断是否是当前月
        if (iMonth == iMonthViewCurrentMonth) {
            txtToDay.setTextColor(Color.parseColor("#ffc2a53d"));
            txtDay.setTextColor(Color.parseColor("#603b07"));
        } else {
            txtDay.setTextColor(Color.parseColor("#c0c0c0"));
            txtToDay.setTextColor(Color.parseColor("#c0c0c0"));
        }

        int day = myDate.getDate(); // 日期
        txtDay.setText(String.valueOf(day));

        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        iv.addView(txtDay, lp);

        LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        iv.addView(txtToDay, lp1);

        return iv;
    }

    @SuppressWarnings("deprecation")
    private Boolean equalsDate(Date date1, Date date2) {
        if (date1.getYear() == date2.getYear()
                && date1.getMonth() == date2.getMonth()
                && date1.getDate() == date2.getDate()) {
            return true;
        } else {
            return false;
        }
    }
}
